home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / inkscape / extensions / extractimage.py < prev    next >
Encoding:
Python Source  |  2010-03-12  |  2.9 KB  |  75 lines

  1. #!/usr/bin/env python 
  2. '''
  3. Copyright (C) 2005 Aaron Spike, aaron@ekips.org
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. '''
  19.  
  20. import inkex, base64, os
  21. import gettext
  22. _ = gettext.gettext
  23.  
  24. class MyEffect(inkex.Effect):
  25.     def __init__(self):
  26.         inkex.Effect.__init__(self)
  27.         self.OptionParser.add_option("--desc")
  28.         self.OptionParser.add_option("--filepath",
  29.                         action="store", type="string", 
  30.                         dest="filepath", default=None,
  31.                         help="")
  32.     def effect(self):
  33.         mimesubext={
  34.             'png' :'.png',
  35.             'bmp' :'.bmp',
  36.             'jpeg':'.jpg',
  37.             'jpg' :'.jpg', #bogus mime
  38.             'icon':'.ico',
  39.             'gif' :'.gif'
  40.         }
  41.         
  42.         # exbed the first embedded image
  43.         path = self.options.filepath
  44.         if (path != ''):
  45.             if (self.options.ids):
  46.                 for id, node in self.selected.iteritems():
  47.                     if node.tag == inkex.addNS('image','svg'):
  48.                         xlink = node.get(inkex.addNS('href','xlink'))
  49.                         if (xlink[:4]=='data'):
  50.                             comma = xlink.find(',')
  51.                             if comma>0:
  52.                                 #get extension
  53.                                 fileext=''
  54.                                 semicolon = xlink.find(';')
  55.                                 if semicolon>0:
  56.                                     for sub in mimesubext.keys():
  57.                                         if sub in xlink[5:semicolon]:
  58.                                             fileext=mimesubext[sub]
  59.                                             path=path+fileext;
  60.                                             break
  61.                                 #save
  62.                                 data = base64.decodestring(xlink[comma:])
  63.                                 open(path,'wb').write(data)
  64.                                 node.set(inkex.addNS('href','xlink'),os.path.realpath(path)) #absolute for making in-mem cycles work
  65.                             else:
  66.                                 inkex.errormsg(_('Unable to find image data.'))
  67.                             break
  68.  
  69. if __name__ == '__main__':
  70.     e = MyEffect()
  71.     e.affect()
  72.  
  73.  
  74. # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99
  75.